package com.zzq.provider.controller;

import com.zzq.provider.bean.Student;
import com.zzq.provider.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author zzq
 * @Date 2020/2/26 17:48
 * @Version 1.0
 */
@RestController
@RequestMapping("/provider/students")
public class ProviderController {

    @Autowired
    private DiscoveryClient client;

    @Autowired
    private StudentService service;

    @PostMapping("/save")
    public boolean save(@RequestBody Student student) {
        return service.saveStudent(student);
    }

    @DeleteMapping("/remove/{id}")
    public boolean remove(@PathVariable Integer id) {
        return service.deleteStudent(id);
    }

    @PutMapping("/update")
    public boolean update(@RequestBody Student student) {
        return service.updateStudent(student);
    }

    @GetMapping("/get/{id}")
    public Student getOne(@PathVariable Integer id){
        return service.getStudent(id);
    }

    @GetMapping("/list")
    public List<Student> list(){
        return service.findAll();
    }

    @GetMapping("/getService")
    public Object list3(){
        List<String> services = client.getServices();
        for (String e:services) {
            List<ServiceInstance> instances = client.getInstances(e);
            for(ServiceInstance instance:instances) {
                System.out.println(instance.getHost()+"  :  "+instance.getPort());
            }
        }
        return services;
    }

}
